/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/22
 * Time: 下午1:29
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Map;

import java.io.InputStream;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.cake21.uitest.System.Core.Config;

public class ConfigMap {

    /*基础配置*/
    private static ConcurrentHashMap<String, HashMap<String, String>> map = initMap();

    public static ConcurrentHashMap<String, HashMap<String, String>> getMap() {
        return map;
    }

    /**
     * initMap: 初始化基础配置表
     *
     * @return map 基础配置表
     */
    private static ConcurrentHashMap<String, HashMap<String, String>> initMap() {

        try {
            // 读取XML文件
            InputStream is = BrowserMap.class.getClassLoader().getResourceAsStream("base.xml");
            NodeList configList = new Config(is).readTags("config");
            ConcurrentHashMap<String, HashMap<String, String>> configMap = new ConcurrentHashMap<String, HashMap<String, String>>();
            for (int i = 0; i < configList.getLength(); i++) {
                // 获取所有配置组的全部配置信息
                NodeList propertyList = configList.item(i).getChildNodes();
                HashMap<String, String> propertyMap = new HashMap<String, String>();
                String groupName = Config.getNodeString(configList.item(i).getAttributes().getNamedItem("group"));
                if (groupName.equals("")) {
                    // 配置组名错误
                    continue;
                }
                for (int j = 0; j < propertyList.getLength(); j++) {
                    // 获取所有配置节点的值
                    if (propertyList.item(j).getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    String propertyName = propertyList.item(j).getNodeName().toString();
                    String propertyValue = propertyList.item(j).getFirstChild().getNodeValue().toString();
                    propertyMap.put(propertyName, propertyValue);
                }
                configMap.put(groupName, propertyMap);
            }
            return configMap;
        } catch (Exception e) {
            return null;
        }
    }
}
